package com.supercell.service;

import com.supercell.dao.DepartmentDAO;
import com.supercell.dao.PersonDAO;
import com.supercell.entity.Department;
import com.supercell.entity.Person;
import com.supercell.misc.util.EntityParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by WUJO2 on 7/15/2016.
 */
@Service
public class DataManipulationService {
    @Value("#{configProps[BIRTH_DATE_FORMAT]}")
    private String birthDateFormat;
    @Value("#{configProps[PERSON_RECORD_BANNER]}")
    private String personRecordBanner;
    @Value("#{configProps[PERSON_RECORD_FORMAT]}")
    private String personRecordFormat;
    @Value("#{configProps[DEPARTMENT_RECORD_BANNER]}")
    private String departmentRecordBanner;
    @Value("#{configProps[DEPARTMENT_RECORD_FORMAT]}")
    private String departmentRecordFormat;
    @Value("#{configProps[PERSON_RECORD_SEPARATOR]}")
    private String personRecordSeparator;
    @Value("#{configProps[DEPARTMENT_RECORD_SEPARATOR]}")
    private String departmentRecordSeparator;
    @Autowired
    private PersonDAO personDAO;
    @Autowired
    private DepartmentDAO departmentDAO;

    public String[] executeCommand(String command) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(birthDateFormat);
        try {
            if (command.startsWith("A")) {
                boolean successful;
                if (command.charAt(2) == 'P') {
                    successful = (personDAO.save((Person) EntityParser.parse(command.substring(4), simpleDateFormat, Person.class)) != null);
                } else {
                    successful = (departmentDAO.save((Department) EntityParser.parse(command.substring(4), simpleDateFormat, Department.class)) != null);
                }
                if (successful) {
                    return new String[]{("Command is processed successfully.")};
                }
            } else if (command.startsWith("U")) {
                if (command.charAt(2) == 'P') {
                    personDAO.update((Person) EntityParser.parse(command.substring(4), simpleDateFormat, Person.class));
                } else {
                    departmentDAO.update((Department) EntityParser.parse(command.substring(4), simpleDateFormat, Department.class));
                }
                return new String[]{("Command is processed successfully.")};
            } else if (command.startsWith("D")) {
                if (command.charAt(2) == 'P') {
                    personDAO.remove(Integer.valueOf(command.substring(7)));
                } else {
                    departmentDAO.remove(Integer.valueOf(command.substring(7)));
                }
                return new String[]{("Command is processed successfully.")};
            } else if (command.startsWith("L")) {
                String[] results = null;
                if (command.charAt(2) == 'P') {
                    List<Person> list = personDAO.getAll();
                    if (!list.isEmpty()) {
                        results = new String[list.size() + 2];
                        Person person;
                        results[0] = personRecordBanner;
                        results[1] = personRecordSeparator;
                        for (int i = 0; i < list.size(); i++) {
                            person = list.get(i);
                            results[i + 2] =
                                    String.format(
                                            personRecordFormat,
                                            person.getId(),
                                            person.getName(),
                                            (person.getBirthday() == null) ? "null" : simpleDateFormat.format(person.getBirthday()),
                                            (person.getTelephone() == null) ? "null" : person.getTelephone(),
                                            (person.getSalary() == null) ? "null" : String.valueOf(person.getSalary()),
                                            person.getDepartmentID());
                        }
                    }
                } else {
                    List<Department> list = departmentDAO.getAll();
                    if (!list.isEmpty()) {
                        results = new String[list.size() + 2];
                        Department department;
                        results[0] = departmentRecordBanner;
                        results[1] = departmentRecordSeparator;
                        for (int i = 0; i < list.size(); i++) {
                            department = list.get(i);
                            results[i + 2] =
                                    String.format(
                                            departmentRecordFormat,
                                            department.getId(),
                                            department.getDepartmentName(),
                                            department.getCity());
                        }
                    }
                }
                return (results == null) ? new String[]{"No result."} : results;
            }
            return new String[]{"Unable to process command: " + command};
        } catch (Exception ignored) {
            return new String[]{"Unable to process command: " + command};
        }
    }
}
